package cn.gjxblog.modules.sys.service.impl;

import cn.gjxblog.common.model.PageInfo;
import cn.gjxblog.common.model.PageRequest;
import cn.gjxblog.modules.sys.dao.SysMenuDao;
import cn.gjxblog.modules.sys.entity.SysMenuEntity;
import cn.gjxblog.modules.sys.model.vo.MenuListRequest;
import cn.gjxblog.modules.sys.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 菜单管理服务实现类
 *
 * @author guojiaxun
 * @date 2019-09-17 11:32:47
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
    @Override
    public PageInfo<SysMenuEntity> searchList(PageRequest request) {

        QueryWrapper<SysMenuEntity> ew = new QueryWrapper<SysMenuEntity>().eq("parent_id", "0").in("type","0","1").orderByAsc("order_num");
        List<SysMenuEntity> data = baseMapper.selectList(ew);
        // 构造权限树
        buildPermissionMenuTree(data);

        IPage<SysMenuEntity> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()),ew);
        return new PageInfo<>(page);
    }

    @Override
    public List<SysMenuEntity> getPermissionMenu(String uid) {
        QueryWrapper<SysMenuEntity> ew = new QueryWrapper<SysMenuEntity>().eq("parent_id", "0").in("type","0","1").orderByAsc("order_num");
        List<SysMenuEntity> data = baseMapper.selectList(ew);
        // 构造权限树
        buildPermissionMenuTree(data);
        return data;
    }

    @Override
    public List<SysMenuEntity> listMenu(MenuListRequest requeset) {
        QueryWrapper<SysMenuEntity> ew = new QueryWrapper<>();
        if(StringUtils.isBlank(requeset.getMenuName())) {
           ew.eq("parent_id", "0").orderByAsc("order_num");
        } else {
           ew.like("name",requeset.getMenuName()).in("type","1").orderByAsc("order_num");
        }
        List<SysMenuEntity> data = baseMapper.selectList(ew);
        // 构造权限树
        buildPermissionMenuTree2(data);
        return data;
    }

    private void buildPermissionMenuTree2(List<SysMenuEntity> data) {
        for (SysMenuEntity entity : data) {
            QueryWrapper<SysMenuEntity> ew = new QueryWrapper<SysMenuEntity>().eq("parent_id", entity.getMenuId()).orderByAsc("order_num");
            List<SysMenuEntity> result = baseMapper.selectList(ew);
            if(result.size() < 1){
                continue;
            }
            buildPermissionMenuTree2(result);
            entity.setChildren(result);
        }
    }

    private void buildPermissionMenuTree(List<SysMenuEntity> data){
        for (SysMenuEntity entity : data) {
            QueryWrapper<SysMenuEntity> ew = new QueryWrapper<SysMenuEntity>().eq("parent_id", entity.getMenuId()).in("type","0","1").orderByAsc("order_num");
            List<SysMenuEntity> result = baseMapper.selectList(ew);
            if(result.size() < 1){
                continue;
            }
            buildPermissionMenuTree(result);
            entity.setChildren(result);
        }
    }
}
